package cn.iocoder.yudao.module.cmt.controller.admin.service;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.service.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.service.ServiceCategoryDO;
import cn.iocoder.yudao.module.cmt.service.service.ServiceCategoryService;

@Tag(name = "管理后台 - 服务分类")
@RestController
@RequestMapping("/cmt/service-category")
@Validated
public class ServiceCategoryController {

    @Resource
    private ServiceCategoryService serviceCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建服务分类")
    @PreAuthorize("@ss.hasPermission('cmt:service-category:create')")
    public CommonResult<Long> createServiceCategory(@Valid @RequestBody ServiceCategorySaveReqVO createReqVO) {
        return success(serviceCategoryService.createServiceCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务分类")
    @PreAuthorize("@ss.hasPermission('cmt:service-category:update')")
    public CommonResult<Boolean> updateServiceCategory(@Valid @RequestBody ServiceCategorySaveReqVO updateReqVO) {
        serviceCategoryService.updateServiceCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:service-category:delete')")
    public CommonResult<Boolean> deleteServiceCategory(@RequestParam("id") Long id) {
        serviceCategoryService.deleteServiceCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除服务分类")
    @PreAuthorize("@ss.hasPermission('cmt:service-category:delete')")
    public CommonResult<Boolean> deleteServiceCategoryList(@RequestParam("ids") List<Long> ids) {
        serviceCategoryService.deleteServiceCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:service-category:query')")
    public CommonResult<ServiceCategoryRespVO> getServiceCategory(@RequestParam("id") Long id) {
        ServiceCategoryDO serviceCategory = serviceCategoryService.getServiceCategory(id);
        return success(BeanUtils.toBean(serviceCategory, ServiceCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务分类分页")
    @PreAuthorize("@ss.hasPermission('cmt:service-category:query')")
    public CommonResult<PageResult<ServiceCategoryRespVO>> getServiceCategoryPage(@Valid ServiceCategoryPageReqVO pageReqVO) {
        PageResult<ServiceCategoryDO> pageResult = serviceCategoryService.getServiceCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务分类 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:service-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServiceCategoryExcel(@Valid ServiceCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceCategoryDO> list = serviceCategoryService.getServiceCategoryPage(pageReqVO).getList();
        ExcelUtils.write(response, "服务分类.xls", "数据", ServiceCategoryRespVO.class,
                        BeanUtils.toBean(list, ServiceCategoryRespVO.class));
    }

}
